package com.erb.config.interceptor;

import com.erb.constants.AdminConstants;
import com.erb.entity.Admin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 后台登录验证session拦截器
 */
@Component
public class AdminLoginInterceptor implements HandlerInterceptor {
	/**
	 * 验证session管理员信息
	 *
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();

		// 获取session中的对象
		Admin sessionAdmin = (Admin) session.getAttribute(AdminConstants.SESSION_ADMIN_KEY);
		if (null == sessionAdmin) {
			// 未登录，去往登录页
			response.sendRedirect(request.getContextPath() + "/admin/login");
			return false;
		}

		return true;
	}
}
